package com.hyphenate.easeui.utils;

import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.support.v4.content.FileProvider;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Description:
 * Created by Andy Hong on 12/27/2016.
 */

public class MediaFileManager {
    private static final String POSTFIX = ".JPEG";
    private static final String APP_NAME = "gjb6";
    private static final String IMAGE_PATH = File.separator + APP_NAME + File.separator + "image" + File.separator;
    /**
     * android 7.0适配  在AndroidManifest 声明（<provider>）
     * android:authorities="com.boostfield.musicbible.provider"
     */
    private static final String AUTHORITIES = "com.gjb6.master.provider";

    public static Uri createMediaUri(Context context, File imageFile) {
        Uri uri;
        if (Build.VERSION.SDK_INT > 23) {
            uri = FileProvider.getUriForFile(context, AUTHORITIES, imageFile);
        } else {
            uri = Uri.fromFile(imageFile);
        }

        return uri;
    }

    public static File createImageFile(Context context) {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA).format(new Date());

        File rootFile = getImageRootDir(context);
        if (!rootFile.exists()) {
            rootFile.mkdirs();
        }
        return new File(rootFile, "IMAGE" + "_" + timeStamp + POSTFIX);

    }

    /**
     * 如果没有外存储 不使用IMAGE_PATH 声明的路径
     * 路径一定要跟res/xml 文件下的provider_paths路径一一对应
     *
     * @param context
     * @return
     */
    private static File getImageRootDir(Context context) {
        String state = Environment.getExternalStorageState();
        return Environment.MEDIA_MOUNTED.equals(state) ?
                new File(Environment.getExternalStorageDirectory() + IMAGE_PATH) :
                new File(context.getCacheDir() + IMAGE_PATH);
    }
}
